package org.kazuhiko.pokemon3d.game;

import java.text.DecimalFormat;
import java.util.ArrayList;

import org.kazuhiko.pokemon3d.entity.Drawable;
import org.kazuhiko.pokemon3d.entity.Flag;
import org.kazuhiko.pokemon3d.utility.camera.PlayerCamera;
import org.kazuhiko.pokemon3d.utility.tools.Debugger;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;

public class World
{
	private PlayerCamera camera;
	
	private Drawable player;
	
	private DecimalFormat		formatter			= new DecimalFormat( "#.##" );
	
	private ArrayList<Drawable> drawables;
	private ArrayList<Drawable> delDrawables;
	
	private Debugger debugger = Debugger.getInstance();
	
	public World( PlayerCamera camera )
	{		
		this.camera = camera;
		drawables = new ArrayList<Drawable>();
		delDrawables = new ArrayList<Drawable>();
		if( camera != null )
		{
			camera.setWorld( this );
		}
	}
	
	public void tick()
	{
		for( Drawable entity : drawables )
		{
			entity.tick();
		}
	}
	
	public void update( int delta )
	{
		camera.update( delta );
		
		for( Drawable entity : delDrawables )
		{
			System.out.println( "deleting " + entity.getName() );
			drawables.remove( entity );
		}
		
		delDrawables.clear();
		
		for( Drawable entity : drawables )
		{
			entity.update();
			
			debugger.addDebugText( "Entity", Color.red );
			debugger.addDebugText( "ID: #" + entity.getId(), Color.yellow );
			debugger.addDebugText( "Name: " + entity.getName(), Color.yellow );
			debugger.addDebugText( "X: " + formatter.format( entity.getPosition().getX() ), Color.yellow );
			debugger.addDebugText( "Y: " + formatter.format( entity.getPosition().getY() ), Color.yellow );
			debugger.addDebugText( "Z: " + formatter.format( entity.getPosition().getZ() ), Color.yellow );
			debugger.addDebugText( "Pitch: " + formatter.format( entity.getRotation().x ), Color.yellow );
			debugger.addDebugText( "Yaw: " + formatter.format( entity.getRotation().y ), Color.yellow );
			debugger.addDebugText( "Roll: " + formatter.format( entity.getRotation().z ), Color.yellow );
			
			if( entity.hasFlag( Flag.REMOVE ) )
			{
				delDrawables.add( entity );
			}
		}
	}
	
	public void render()
	{
		camera.translateCamera();
		for( Drawable entity : drawables )
		{
			if( !entity.hasFlag( Flag.DO_NOT_RENDER ) )
			{
				GL11.glColor3f( 1f, 1f, 1f );
				entity.render();
			}
		}
	}
	
	public void setPlayer( Drawable player )
	{
		this.player = player;
	}
	
	public Drawable getPlayer()
	{
		return player;
	}
	
	public void addEntity( Drawable entity )
	{
		drawables.add( entity );
	}
	
	public Drawable getEntityById( int id )
	{
		Drawable ret = null;
		
		for( int i = 0; i < drawables.size() && ret == null; i++ )
		{
			if( drawables.get( i ).getId() == id )
			{
				ret = drawables.get( i );
			}
		}
		
		return ret;
	}
	
	public PlayerCamera getCamera()
	{
		return camera;
	}
	
	public void setCamera( PlayerCamera camera )
	{
		this.camera = camera;
	}
	
	public ArrayList<Drawable> getDrawables()
	{
		return drawables;
	}
}
